package cn.wolfcode.crm.shiro;


import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import lombok.Setter;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.*;

public class EmployeeRealm extends AuthorizingRealm {
    @Setter
    private IEmployeeService employeeService;

    @Setter
    private IRoleService roleService;

    @Setter
    private IPermissionService permissionService;

    public String getName(){
        return "EmployeeRealm";
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals) {
       /* //1:获取当前登录的对象
        Employee currentUser = (Employee) principals.getPrimaryPrincipal();
        //2:通过用户名查询过数据库,返回当前用户的角色与权限
        List<String> roles = new ArrayList<>();
        List<String> permisssions=new ArrayList<>();

        //3:将角色与权限封装到info对象中,让shiro进行下一步角色与权限判断
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        if (currentUser.isAdmin()) {
            List<Role> list = roleService.selectAll();
            for (Role role :list){
                roles.add(role.getSn());
            }
            //权限
            permisssions.add("*:*");
        }else {
            roles = roleService.selectByEmpId(currentUser.getId());
            //根据登录用户id查询用户的权限
            permisssions = permissionService.queryPermissionResourceByEmpId(currentUser.getId());
        }

        info.addRoles(roles);
        info.addStringPermissions(permisssions);
        return info;*/

       //数据写死
        Employee user = (Employee) principals.getPrimaryPrincipal();

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //查询数据库获取角色和权限信息
        if(user.isAdmin()){
            info.addStringPermission("*:*");
        }else{
            info.addRoles(Arrays.asList("hr"));
            info.addStringPermissions(Arrays.asList("employee:index","employee:list"));
        }

        return info;


    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException {
        //1:获取页面传入的用户名
        String uname = (String) token.getPrincipal();

        //2:查询数据库,找到该名字,封装创建employee对象
        //执行sql
        Employee employee = employeeService.selectUsername(uname);
        if (employee == null) {
            return null;
        }

        //3:将查询出来的用户封装成info对象,交给shiro进行其他处理
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(employee, employee.getPassword(), getName());

        return info;
    }
}
